package com.bstek.uflo.form.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.SerializationUtils;
import org.springframework.web.servlet.ModelAndView;

import com.bstek.dorado.view.View;
import com.bstek.dorado.view.resolver.PageOutputUtils;
import com.bstek.dorado.web.resolver.AbstractResolver;
import com.bstek.uflo.form.model.Form;

/**
 * @author Jacky.gao
 * @since 2013年11月6日
 */
public class FormTemplateController extends AbstractResolver {
	@Override
	protected ModelAndView doHandleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
		Form page=FormHolder.getForm();
		if(page==null){
			throw new IllegalArgumentException("Invalid Request.");
		}
		response.setContentType("text/html; charset=utf-8");
		PrintWriter writer=response.getWriter();
		String content=(String)SerializationUtils.deserialize(page.getContent());
		StringBuffer sb=new StringBuffer();
		sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
		sb.append("<html>\n");
		sb.append("<head>\n");
		sb.append("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=IE8\"></meta>\n");
		writer.write(sb.toString());
		View view = PageOutputUtils.getView(request);
		PageOutputUtils.outputHeader(view,request,response,response.getWriter());
		StringBuffer sb1=new StringBuffer();
		sb1.append("</head>\n");
		sb1.append("<body style=\"font-size:12px;margin:1px\">\n");
		sb1.append(content);
		sb1.append("</body>\n");
		sb1.append("</html>\n");
		writer.write(sb1.toString());
		writer.flush();
		writer.close();
		return null;
	}
}
